package com.sh.data.engine.web.webapi.controller.datadev;

import com.sh.data.engine.application.model.dto.base.PagerResponse;
import com.sh.data.engine.application.model.dto.datadev.OfflineDevNodeResponse;
import com.sh.data.engine.application.model.dto.datadev.OfflineDevOpeartionSummaryResponse;
import com.sh.data.engine.application.model.dto.datadev.OfflineDevOperationJobResponse;
import com.sh.data.engine.application.model.dto.datadev.OfflineDevTaskLogResponse;
import com.sh.data.engine.application.model.dto.workspace.UserResponse;
import com.sh.data.engine.application.model.request.datadev.OfflineDevOperationJobSearchRequest;
import com.sh.data.engine.application.model.request.datadev.OfflineDevTaskAlarmRequest;
import com.sh.data.engine.application.service.datadev.OfflineDevOperationApplicationService;
import com.sh.data.engine.domain.annotation.LogExecution;
import com.sh.data.engine.domain.annotation.userlog.Business;
import com.sh.data.engine.domain.annotation.userlog.EngineOperation;
import com.sh.data.engine.domain.datadev.offline.model.domain.OfflineDevOperationTaskflowDetailInfoDomain;
import com.sh.data.engine.web.webapi.controller.base.response.RestResponseEntity;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/offlineDev/operation")
@Tag(name = "数据开发-离线计算-任务运维", description = "离线开发相关API-运维中心")
@Slf4j
public class OfflineDevOperationController {

    @Autowired
    private OfflineDevOperationApplicationService offlineDevOperationApplicationService;

    @Operation(summary = "获取任务的统计数-head")
    @GetMapping("/summary")
//    @AuthorityAnnotation(menuId = MenuEnumNew.RWYW, readWriteStatus = 0)
    public RestResponseEntity<OfflineDevOpeartionSummaryResponse> getOfflineDevSummay(
        @Parameter(description = "nodeId 历史任务中使用") @RequestParam(required = false) Long nodeId) {
        var result = offlineDevOperationApplicationService.getOfflineDevSummary(nodeId);
        return RestResponseEntity.success(result);
    }

    @Operation(summary = "查询任务列表")
    @GetMapping("/jobList")
//    @AuthorityAnnotation(menuId = MenuEnumNew.RWYW, readWriteStatus = 0)
    public RestResponseEntity<PagerResponse<OfflineDevOperationJobResponse>>
    getOfflineDevOperationJobPage(OfflineDevOperationJobSearchRequest searchRequest) {
        var result = offlineDevOperationApplicationService.getOfflineDevOperationJobPage(searchRequest);
        return RestResponseEntity.success(result);
    }

    @Operation(summary = "查看某个任务的历史任务列表")
    @GetMapping("/jobHistory")
//    @AuthorityAnnotation(menuId = MenuEnumNew.RWYW, readWriteStatus = 0)
    public RestResponseEntity<PagerResponse<OfflineDevOperationJobResponse>> getOfflineDevJobHistoryPage(
        @Parameter(description = "pageNum")
        @RequestParam(value = "pageNum", required = false, defaultValue = "1")
        Integer pageNum,
        @Parameter(description = "pageSize")
        @RequestParam(value = "pageSize", required = false, defaultValue = "10")
        Integer pageSize,
        @Parameter(description = "nodeId", required = true) @RequestParam("nodeId") Long nodeId,
        @Parameter(description = "taskStatus", required = false)
        @RequestParam(value = "taskStatus", required = false)
        Integer taskStatus,
        @Parameter(description = "execType", required = false)
        @RequestParam(value = "execType", required = false)
        Integer execType,
        @Parameter(description = "order", required = false)
        @RequestParam(value = "order", required = false)
        String order,
        @Parameter(description = "orderField", required = false)
        @RequestParam(value = "orderField", required = false)
        String orderField) {
        var result = offlineDevOperationApplicationService.getOfflineDevJobHistoryPage(
            nodeId, taskStatus, execType, order, orderField, pageNum, pageSize);
        return RestResponseEntity.success(result);
    }

    @Operation(summary = "查询子任务 ")
    @GetMapping("/job/children")
//    @AuthorityAnnotation(menuId = MenuEnumNew.RWYW, readWriteStatus = 0)
    public RestResponseEntity<List<OfflineDevOperationJobResponse>> getOfflineDevOperationJobList(
        @Parameter(description = "执行记录jobId", required = true) @RequestParam Long jobId) {
        var result = offlineDevOperationApplicationService.getOfflineDevOperationJobList(jobId);
        return RestResponseEntity.success(result);
    }

    @GetMapping("/task/log")
    @Operation(summary = "查询日志")
//    @AuthorityAnnotation(menuId = MenuEnumNew.RWYW, readWriteStatus = 0)
    public RestResponseEntity<OfflineDevTaskLogResponse> taskLog(
        @Parameter(description = "任务id", required = true) @RequestParam("jobId") Long jobId,
        @Parameter(description = "偏移量", required = true) @RequestParam("offset") Integer offset,
        @Parameter(description = "") @RequestParam(value = "rsHasShow", required = false) String rsHasShow) {
        var result = offlineDevOperationApplicationService.taskLog(jobId, offset, rsHasShow);
        return RestResponseEntity.success(result);
    }

    @GetMapping("/task/restart")
    @Operation(summary = "重跑任务")
    @LogExecution(operation = EngineOperation.UPLOAD, business = Business.实时任务)
//    @Log(
//        business = Business.离线开发任务,
//        operation = Operation.START_TASK)
//    @AuthorityAnnotation(menuId = MenuEnumNew.RWYW, readWriteStatus = 1)
    public RestResponseEntity<Long> taskRestart(
        @Parameter(description = "jobId", required = true) @RequestParam("id") Long id) {
        var result = offlineDevOperationApplicationService.taskRestart(id);
        return RestResponseEntity.success(result);
    }

    @PostMapping("/task/stop")
    @Operation(summary = "停止任务")
    @LogExecution(operation = EngineOperation.UPLOAD, business = Business.实时任务)
//    @Log(
//        business = Business.离线开发任务,
//        operation = Operation.STOP_TASK)
//    @AuthorityAnnotation(menuId = MenuEnumNew.RWYW, readWriteStatus = 1)
    public RestResponseEntity<Boolean> taskStop(@RequestParam("logId") Long logId) {
        var result = offlineDevOperationApplicationService.taskStop(logId);
        return RestResponseEntity.success(result);
    }

    @GetMapping("/task/detail")
//    @AuthorityAnnotation(menuId = MenuEnumNew.RWYW, readWriteStatus = 0)
    @Operation(summary = "任务详情")
    public RestResponseEntity<OfflineDevOperationTaskflowDetailInfoDomain>
    getOfflineDevTaskFlowDetailDomain(
        @Parameter(description = "执行任务的job id", required = true) @RequestParam("nodeId") Long nodeId) {
        var result = offlineDevOperationApplicationService.getOfflineDevTaskFlowDetailDomain(nodeId);
        return RestResponseEntity.success(result);
    }

    @GetMapping("/node/detail")
    @Operation(summary = "节点详情")
//    @AuthorityAnnotation(menuId = MenuEnumNew.RWYW, readWriteStatus = 0)
    public RestResponseEntity<Object> getNodeDetail(
        @Parameter(description = "执nodeId", required = true) @RequestParam("nodeId") Long nodeId)
        throws IOException {
        var result = offlineDevOperationApplicationService.getNodeDetail(nodeId);
        return RestResponseEntity.success(result);
    }

    @PostMapping("/taskAlarm/save")
    @Operation(summary = "保存监控")
    @LogExecution(operation = EngineOperation.UPLOAD, business = Business.实时任务)
//    @Log(
//        business = Business.告警配置,
//        operation = Operation.ADD_UPDATE)
//    @AuthorityAnnotation(menuId = MenuEnumNew.RWYW, readWriteStatus = 1)
    public RestResponseEntity<Boolean> saveOfflineDevTaskAlarm(
        @RequestBody OfflineDevTaskAlarmRequest offlineDevTaskAlarmRequest) {
        var result = offlineDevOperationApplicationService.saveOfflineDevTaskAlarm(offlineDevTaskAlarmRequest);
        return RestResponseEntity.success(result);
    }

    @GetMapping("/getAllTaskFlow")
    @Operation(summary = "获取所工作流信息")
//    @AuthorityAnnotation(menuId = MenuEnumNew.RWYW, readWriteStatus = 0)
    public RestResponseEntity<List<OfflineDevNodeResponse>> getAllTaskFlowList() {
        var result = offlineDevOperationApplicationService.getAllTaskFlowList();
        return RestResponseEntity.success(result);
    }

    @GetMapping("/getCallUserInfo")
    @Operation(summary = "获取所以通知用户信息")
//    @AuthorityAnnotation(menuId = MenuEnumNew.RWYW, readWriteStatus = 0)
    public RestResponseEntity<List<UserResponse>> getCallUserInfo() {
        var result = offlineDevOperationApplicationService.getCallUserInfo();
        return RestResponseEntity.success(result);
    }

    @GetMapping("/getParentType")
//    @AuthorityAnnotation(menuId = MenuEnumNew.RWYW, readWriteStatus = 0)
    public RestResponseEntity<Long> getParentTypeByNodeId(Long nodeId) {
        var result = offlineDevOperationApplicationService.getParentTypeByNodeId(nodeId);
        return RestResponseEntity.success(result);
    }
}
